#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickWindow>
#include "system.h"
#include "LineChartItem.h"
#include "user.h"
#include "db.h"
#include "test.h"
#include "setting.h"
#include "collection.h"
#include "alarm.h"
#include "global.h"
#include "batch.h"
#include "logmanager.h"
#include "operation.h"
#include "batchdata.h"
#include "warn.h"

bool init(QString &error){
    LogManager::ins().initialize();
    //qmlRegisterType<Test>("ui.pageMain",1,0, "Test");
    // 注册自定义 QML 类型
    qmlRegisterType<LineChartItem>("CustomCharts", 1, 0, "LineChart");
    DB *db = DB::getInstance();
    db->init();

    return true;
}

void testWindow(){
    QQuickWindow window;

    // 获取图形后端
    QSGRendererInterface::GraphicsApi api = window.graphicsApi();

    switch (api) {
    case QSGRendererInterface::OpenGL:
        qDebug() << "==============当前使用 OpenGL 后端";
        break;
    case QSGRendererInterface::Direct3D11:
        qDebug() << "==============当前使用 Direct3D 11 后端";
        break;
    case QSGRendererInterface::Vulkan:
        qDebug() << "==============当前使用 Vulkan 后端";
        break;
    case QSGRendererInterface::Metal:
        qDebug() << "==============当前使用 Metal 后端";
        break;
    case QSGRendererInterface::Software:
        qDebug() << "==============当前使用软件渲染后端";
        break;
    default:
        qDebug() << "==============未知图形后端";
        break;
    }
}

int main(int argc, char *argv[])
{
    // // 启用 Qt 虚拟键盘输入法插件
    // qputenv("QT_IM_MODULE", "qtvirtualkeyboard");
    // // 强制使用 Fusion 样式以支持自定义控件背景等
    // qputenv("QT_QUICK_CONTROLS_STYLE", "Fusion");
    //qputenv("QT_QUICK_BACKEND", "software");
    QGuiApplication app(argc, argv);


    // 创建系统信息逻辑实例
    QString error;
    if (!init(error)) QCoreApplication::exit(-2);
    //testWindow();

    // 实例注册到QML上下文
    System          system;
    Setting         setting;
    Alarm           alarm;
    User            user;
    Test            test;
    Collection      collection;
    Batch           batch;
    BatchData       batchData;
    Operation       operation;
    Warn            warn;


    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("systemInfo", &system);
    engine.rootContext()->setContextProperty("userInfo", &user);
    engine.rootContext()->setContextProperty("settingInfo", &setting);
    engine.rootContext()->setContextProperty("alarmInfo", &alarm);
    engine.rootContext()->setContextProperty("collectionInfo", &collection);
    engine.rootContext()->setContextProperty("batchInfo", &batch);
    engine.rootContext()->setContextProperty("batchdataInfo", &batchData);
    engine.rootContext()->setContextProperty("operationInfo", &operation);
    engine.rootContext()->setContextProperty("warnInfo", &warn);
    engine.rootContext()->setContextProperty("test", &test);


    Global::instance().add("settingInfo",&setting);
    Global::instance().add("userInfo",&user);
    Global::instance().add("alarmInfo",&alarm);
    Global::instance().add("collectionInfo",&collection);
    Global::instance().add("operationInfo",&operation);
    collection.start();

    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);

    engine.loadFromModule("lepure", "Main");

    return app.exec();
}
